//
//  GNComposeToolBar.swift
//  SinaWeChat
//
//  Created by mac on 15/12/15.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

enum GNComposeToolBarButtonType: Int {
    case Picture = 0   // 图片
    case Mention = 1   // @
    case Trend = 2     // 话题
    case Emotion = 3   // 表情
    case Add = 4       // 加
}

class GNComposeToolBar: UIStackView {

    
    var childButtonClickClosure: ((type: GNComposeToolBarButtonType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //指定排列
        axis = .Horizontal
        //指定子空间大小
        distribution = .FillEqually
        
        //添加子控件
        setupUI()
    
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置图
    private func setupUI() {
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        addChildButton("compose_emoticonbutton_background", type: .Emotion)
        addChildButton("compose_add_background", type: .Add)
        
    }
    
    
    
    private func addChildButton(imgName: String,type: GNComposeToolBarButtonType) {
        let button = UIButton()
        
        //取到枚举的原始值
        button.tag = type.rawValue
        
        button.addTarget(self, action: "childButtonClick:", forControlEvents: .TouchUpInside)
        //设置不同状态的图片
        button.setImage(UIImage(named:imgName), forState: .Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: .Highlighted)
        
        
        //设置背景
        button.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        addArrangedSubview(button)
        
    }
    
    //MARK: - 监听方法
    @objc private func childButtonClick(button:UIButton){
        
        //执行闭包
        //通过原始值初始化一个枚举:枚举名(rawValue: 值)
        childButtonClickClosure?(type:GNComposeToolBarButtonType(rawValue: button.tag)!)
}
    

}
